package repository

import (
	"github.com/jinzhu/gorm"
	"go_shengshu_admin/model"
	"go_shengshu_admin/query"
	"go_shengshu_admin/utils"
	"time"
)

type ProductRepository struct {
	DB *gorm.DB
}

type ProductRepoInterface interface {
	List(req *query.ListQuery) (products []*model.Product, err error)
	GetTotal() (total int64, err error)
	GetProduct(product model.Product) (*model.Product, error)
	Exist(product model.Product) *model.Product
	ExistById(id string) *model.Product
	Add(product model.Product) (*model.Product, error)
	Edit(product model.Product) (bool, error)
	Delete(id string) (bool, error)
}

func (repo *ProductRepository) List(req *query.ListQuery) (products []*model.Product, err error) {
	db := repo.DB
	limit, offset := utils.Page(req.PageSize, req.Page)
	if err := db.Order(utils.Sort(req.Order)).Limit(limit).Offset(offset).Find(&products).Error; err != nil {
		return nil, err
	}
	return products, nil
}

func (repo *ProductRepository) GetTotal() (total int64, err error) {
	var products []model.Product
	db := repo.DB
	if err := db.Find(&products).Count(&total).Error; err != nil {
		return 0, err
	}
	return total, nil

}

func (repo *ProductRepository) GetProduct(product model.Product) (*model.Product, error) {
	if err := repo.DB.Where(&product).Find(&product).Error; err != nil {
		return nil, err
	}
	return &product, nil
}

func (repo *ProductRepository) Exist(product model.Product) *model.Product {
	var count = 0
	repo.DB.Where("product_name = ?", product.ProductName).Find(&product).Count(&count)
	if count > 0 {
		return &product
	}
	return nil
}

func (repo *ProductRepository) ExistById(id string) *model.Product {
	var product *model.Product
	var count = 0
	repo.DB.Find(&product).Where("product_id = ?", id).Count(&count)
	if count > 0 {
		return product
	}
	return nil
}

func (repo *ProductRepository) Add(product model.Product) (*model.Product, error) {
	product.CreateAt, _ = time.Parse(utils.TimeLayout, utils.GetNow())
	product.UpdateAt, _ = time.Parse(utils.TimeLayout, utils.GetNow())
	product.IsDeleted = false
	if err := repo.DB.Create(&product).Error; err != nil {
		return nil, err
	}
	return &product, nil
}

func (repo *ProductRepository) Edit(product model.Product) (bool, error) {
	if err := repo.DB.Model(&product).Where("product_id = ?", product.ProductId).Updates(map[string]interface{}{
		"product_name":           product.ProductName,
		"product_intro":          product.ProductIntro,
		"category_id":            product.CategoryId,
		"product_cover_url":      product.ProductCoverImg,
		"origin_price":           product.OriginPrice,
		"selling_price":          product.SellingPrice,
		"stock_num":              product.StockNum,
		"tag":                    product.Tag,
		"sell_status":            product.SellStatus,
		"product_detial_content": product.ProductDetailContent,
	}).Error; err != nil {
		return false, err
	}
	return true, nil
}

func (repo *ProductRepository) Delete(id string) (bool, error) {
	var product *model.Product
	if err := repo.DB.Model(&product).Where("product_id = ?", id).Update("is_deleted", false).Error; err != nil {
		return false, err
	}
	return true, nil
}
